import router from "@/router/index.ts";
import { useRouter } from "vue-router";

const $router = useRouter();

// 定义全部的路由配置：
export const allRoutes = [
  {
    id: "1",
    name: "WorkDesk",
    path: "WorkDesk",
    component: () => import("@/views/Admin/WorkDesk.vue"),
  },
  {
    id: "2",
    name: "Home",
    path: "Home",
    component: () => import("@/views/Admin/Home.vue"),
  },
  {
    id: "3-1",
    name: "BannerList",
    path: "BannerList",
    component: () => import("@/views/Admin/Banner/List.vue"),
    meta: {
      id: "3-1",
    },
  },
  {
    id: "3-2",
    name: "BannerAdd",
    path: "BannerAdd",
    component: () => import("@/views/Admin/Banner/AddBanner.vue"),
    meta: {
      id: "3-2",
    },
  },
  {
    id: "4-1",
    name: "OperList",
    path: "OperList",
    component: () => import("@/views/Admin/Operator/OperList.vue"),
  },
  {
    id: "4-2",
    name: "AddOper",
    path: "AddOper",
    component: () => import("@/views/Admin/Operator/AddOper.vue"),
  },
  {
    id: "5-1",
    name: "SearchGoods",
    path: "SearchGoods",
    component: () => import("@/views/Admin/Goods/SearchGoods.vue"),
  },
  {
    id: "6",
    name: "EchartsDemo",
    path: "EchartsDemo",
    component: () => import("@/views/Admin/EchartsDemo/index.vue"),
  },
  {
    id: "7",
    name: "RichTextDemo",
    path: "RichTextDemo",
    component: () => import("@/views/Admin/RichTextDemo/index.vue"),
  },
  {
    id: "8",
    name: "MapDemo",
    path: "MapDemo",
    component: () => import("@/views/Admin/MapDemo/index.vue"),
  }
];

// 根据当前用户的权限，过滤出它对应的路由配置
export const getCurrentRoutes = () => {
  // 1、获取当前用户的权限数据
  if (
    sessionStorage.getItem("checkedkeys") == null ||
    sessionStorage.getItem("checkedkeys") == undefined
  ) {
    alert("亲，请求登录！");
    $router.push("/Login");
    return;
  }

  // 1、获取当前用户的权限数据：["2","3","3-2"]
  let checkedkeys;
  if (sessionStorage.getItem("checkedkeys") == "[]") {
     checkedkeys = [
      "1",
      "2",
      "3-1",
      "3-2",
      "4-1",
      "4-2",
      "5-1",
      "6",
      "7",
      "8",
      "9",
    ];
  } else {
     checkedkeys = JSON.parse(
      sessionStorage.getItem("checkedkeys") as string
    );
  }

  //   根据当前用户的权限，过滤出它对应的路由配置;

  const currRoutes = [];
  allRoutes.forEach((item) => {
    if (checkedkeys.includes(item.id)) {
      currRoutes.push({ ...item });
      router.addRoute("Admin", item);
    }
  });
  console.log("router.getRoutes()", router.getRoutes());

};
